﻿Imports DAL
Imports System.Text.RegularExpressions
Public Class UcGestionPersonneRessource
    Private _ListContact As New List(Of DAL.PersonneRessource)
    Private IndexPrecedent As Integer
    Public Event ajout As eventhandler
    Public IsOnLoad As Boolean
    Private OnCancel As Boolean

    'fonction qui vérifi les informations entrer sur le(s) contact(s)
    Public Function VérifContact()
        Dim verif As Boolean = True
        ErrVal.Clear()
        'vérification du nom de contact vide
        If tbNomContact.Text = "" Then
            ErrVal.SetError(tbNomContact, "Veuillez entrer un nom pour le contact")
            verif = False
        End If

        'vérification du prénom de contact vide
        If tbPrénomContact.Text = "" Then
            ErrVal.SetError(tbPrénomContact, "Veuillez entrer un prénom pour le contact")
            verif = False
        End If

        'vérification de l'adresse de contact valide
        'If Not verifAdresse(tbAdresseContact.Text) Then
        '    ErrVal.SetError(tbAdresseContact, "Veuillez entrer une adresse valide pour le contact")
        '    verif = False
        'Else
        '    ErrVal.SetError(tbAdresseContact, "")
        'End If

        'vérification du code postal de contact valide
        'If Not VerifCP(tbCPContact.Text) Then
        '    ErrVal.SetError(tbCPContact, "Veuillez entrer un code postal valide pour le contact")
        '    verif = False
        'Else
        '    ErrVal.SetError(tbCPContact, "")
        'End If

        'vérification du lien de contact vide
        'If tbLien.Text = "" Then
        '    ErrVal.SetError(tbLien, "Veuillez entrer un lien pour le contact")
        '    verif = False
        'Else
        '    ErrVal.SetError(tbLien, "")
        'End If

        'Si aucune erreur, ajout ou modification des infomations du bénéficiaire
        If verif = True Then
            Return True
        Else
            Return False
        End If
    End Function

    'Fonction permettant de valider les Adresses entrés
    Private Function verifAdresse(ByVal test)
        Dim re As Regex = New Regex("^([\d]+)([\s])(.+)$")
        Return re.IsMatch(test)
    End Function

    'Fonction permettant de valider le code postal entré
    Private Function VerifCP(ByVal test)
        Dim re As Regex = New Regex("^([^DdFfIiOoQqUuWwZz0-9\W])([0-9])([^DdFfIiOoQqUu0-9\W])\s([0-9])([^DdFfIiOoQqUu0-9\W])([0-9])$")
        Return re.IsMatch(test)
    End Function

    Public Property ListContact() As List(Of DAL.PersonneRessource)
        Get
            Return _ListContact
        End Get
        Set(ByVal value As List(Of DAL.PersonneRessource))

        End Set
    End Property

    Public Sub remplire(ByVal BeneficiaireID As Integer)
        For Each contact In (From c In bd.PersonneRessources Where c.Beneficiaire = BeneficiaireID Select c)
            Dim item As New ListViewItem
            item.Text = contact.Nom + ", " + contact.Prenom
            item.Tag = contact.ID
            ListView1.Items.Add(item)
            _ListContact.Add(contact)
        Next
        If ListView1.Items.Count > 0 Then
            ListView1.Items(0).Selected = True
        Else
            tbAdresseContact.Enabled = False
            tbCPContact.Enabled = False
            tbLien.Enabled = False
            tbNomContact.Enabled = False
            tbPrénomContact.Enabled = False
            cbVilleContact.Enabled = False
            Telephone2.Enabled = False
        End If
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        If ListView1.SelectedItems.Count > 0 Then
            If btAjouter.Text = "Ajouter" Or IsOnLoad Then
                Dim contact = ListContact(ListView1.SelectedIndices(0))
                With contact
                    tbAdresseContact.Enabled = True
                    tbCPContact.Enabled = True
                    tbLien.Enabled = True
                    tbNomContact.Enabled = True
                    tbPrénomContact.Enabled = True
                    Telephone2.Enabled = True
                    cbVilleContact.Enabled = True
                    tbAdresseContact.Text = .Adresse
                    tbCPContact.Text = .CodePostal
                    tbLien.Text = .Lien
                    tbNomContact.Text = .Nom
                    tbPrénomContact.Text = .Prenom
                    If Not IsNothing(.Ville) Then cbVilleContact.SelectedValue = .Ville
                    Telephone2.SetTelephones(.Telephones.ToList)
                End With
                btAjouter.Text = "Ajouter"
                BtAnnuler.Enabled = False
            Else
                Select Case MsgBox("Voulez-vous enregistrer les modifications", vbYesNo, "Avertissement")
                    Case vbYes
                        Enregistrer(IndexPrecedent)
                        ListView1_SelectedIndexChanged(Nothing, Nothing)
                    Case vbNo
                        btAjouter.Text = "Ajouter"
                        BtAnnuler.Enabled = False
                        ListView1_SelectedIndexChanged(Nothing, Nothing)
                End Select
            End If
            IndexPrecedent = ListView1.SelectedIndices(0)
            btSupprimer.Enabled = True
        End If
    End Sub

    Private Sub btSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSupprimer.Click
        If MsgBox("Voulez-vous vraiment supprimer", vbYesNo, "Avertissement") = vbYes Then
            _ListContact.RemoveAt(ListView1.SelectedIndices(0))
            ListView1.Items.Remove(ListView1.SelectedItems(0))
            If ListView1.Items.Count > 0 Then
                ListView1.Items(0).Selected = True
            Else
                viderChamps(False)
            End If
            RaiseEvent ajout(True, Nothing)
        End If
    End Sub

    Private Sub btAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAjouter.Click
        If btAjouter.Text = "Ajouter" Then
            IndexPrecedent = -1
            viderChamps(False)
            tbAdresseContact.Enabled = True
            tbCPContact.Enabled = True
            tbLien.Enabled = True
            tbNomContact.Enabled = True
            tbPrénomContact.Enabled = True
            Telephone2.Enabled = True
            cbVilleContact.Enabled = True
            BtAnnuler.Enabled = True
            btSupprimer.Enabled = False
            btAjouter.Text = "Enregistrer"
            ListView1.SelectedItems.Clear()
        Else
            If ListView1.SelectedIndices.Count > 0 Then
                Enregistrer(ListView1.SelectedIndices(0))
            Else
                Enregistrer(-1)
            End If
        End If

    End Sub

    Private Sub UcGestionPersonneRessource_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.DesignMode Then
            Dim listVille As New List(Of Ville)
            For Each Ville In bd.Villes
                listVille.Add(Ville)
            Next
            IsOnLoad = True
            cbVilleContact.DataSource = listVille
            cbVilleContact.DisplayMember = "Nom"
            cbVilleContact.ValueMember = "ID"
            IsOnLoad = False
        End If
    End Sub
    Public Sub viderChamps(ByVal list As Boolean)
        If list Then
            ListContact.Clear()
            ListView1.Items.Clear()
        End If
        tbAdresseContact.Text = String.Empty
        tbCPContact.Text = String.Empty
        tbLien.Text = String.Empty
        tbNomContact.Text = String.Empty
        tbPrénomContact.Text = String.Empty
        Telephone2.SetTelephones(New List(Of Telephone))
        tbAdresseContact.Enabled = False
        tbCPContact.Enabled = False
        tbLien.Enabled = False
        tbNomContact.Enabled = False
        tbPrénomContact.Enabled = False
        cbVilleContact.Enabled = False
        Telephone2.Enabled = False
        btAjouter.Text = "Ajouter"
        BtAnnuler.Enabled = False
        btSupprimer.Enabled = False
    End Sub

    Private Sub cbVilleContact_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbVilleContact.SelectedIndexChanged, tbAdresseContact.TextChanged, tbCPContact.TextChanged, tbLien.TextChanged, tbNomContact.TextChanged, tbPrénomContact.TextChanged, Telephone2.modification
        If Not IsOnLoad Then
            btAjouter.Text = "Enregistrer"
            BtAnnuler.Enabled = True
        End If
    End Sub

    Private Sub Enregistrer(ByVal index)
        If VérifContact() Then
            Dim item As New ListViewItem
            Dim Contact As PersonneRessource
            If index > -1 Then
                Contact = ListContact(index)
            Else
                Contact = New PersonneRessource
            End If
            With Contact
                .Adresse = tbAdresseContact.Text.Trim
                .CodePostal = tbCPContact.Text
                .Lien = tbLien.Text
                .Nom = StrConv(tbNomContact.Text, VbStrConv.ProperCase)
                .Prenom = StrConv(tbPrénomContact.Text, VbStrConv.ProperCase)
                .Ville1 = cbVilleContact.SelectedItem
                Dim telcontact As New System.Data.Linq.EntitySet(Of Telephone)
                telcontact.AddRange(Telephone2.GetTelephones.ToList)
                .Telephones = telcontact
                btAjouter.Text = "Ajouter"
                BtAnnuler.Enabled = False
                If ListView1.SelectedIndices.Count = 0 Then
                    ListContact.Add(Contact)
                    item.Text = Contact.Nom + ", " + Contact.Prenom
                    ListView1.Items.Add(item)
                    ListView1.SelectedItems.Clear()
                    ListView1.Items(ListView1.Items.Count - 1).Selected = True
                Else
                    ListView1.SelectedItems(0).Text = Contact.Nom + ", " + Contact.Prenom
                End If
                RaiseEvent ajout(True, Nothing)
            End With
        Else
            MsgBox("Certaines informations, sur le contact, sont invalides")
        End If
    End Sub

    Private Sub BtAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtAnnuler.Click
        btAjouter.Text = "Ajouter"
        If ListView1.Items.Count > 0 Then
            ListView1.Items(0).Selected = True
        Else
            viderChamps(False)
        End If
    End Sub

End Class
